// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

<!--
function gotop(){
    document.write("<button id='gotop' class='button whitebutton indicator off' type='button'>顶回部到</button>");
    // 回到顶部功能
    jQuery('#gotop').click(function(){
        jQuery('html,body').animate({
            scrollTop: '0px'
        }, 500);
    });
    var n = 0;
    if (!document.getElementById("gotop")) {
        return false;
    }
    var x = 0;
    window.onscroll = function(){
        x = document.documentElement.scrollTop + n;
        if (x == 0) {
            jQuery('#gotop').addClass("off");
        }
        else {
            jQuery('#gotop').removeClass("off");
        };
            };
}

-->

/*
 var tagmate = tagmate ||
 {
 USER_TAG_EXPR: "@\\w+(?: \\w*)?",
 HASH_TAG_EXPR: "#\\w+",
 USD_TAG_EXPR: "\\$(?:(?:\\d{1,3}(?:\\,\\d{3})+)|(?:\\d+))(?:\\.\\d{2})?",
 GBP_TAG_EXPR: "\\\u00a3(?:(?:\\d{1,3}(?:\\,\\d{3})+)|(?:\\d+))(?:\\.\\d{2})?",
 filter_options: function(b, c){
 for (var e = [], g = 0; g < b.length; g++) {
 var f = b[g].label.toLowerCase(), d = c.toLowerCase();
 d.length <= f.length && f.indexOf(d) == 0 && e.push(b[g])
 }
 return e
 },
 sort_options: function(b){
 return b.sort(function(c, e){
 c = c.label.toLowerCase();
 e = e.label.toLowerCase();
 if (c >
 e)
 return 1;
 else
 if (c < e)
 return -1;
 return 0
 })
 }
 };
 */
var EditPin = function(){
    return {
        setup: function(){
            jQuery("#description_pin_edit").keyup(function(){
                jQuery("#postDescription").html(jQuery(this).val())
            })
        },
        deletePin: function(){
            var b = jQuery("#DeletePin .SubmitButton");
            b.addClass("disabled").text(b.data("text-deleting"));
            jQuery.post("/pin/delete/" + pinID, {}, function(c){
                if (c.status == "success") {
                    trackGAEvent("delete_pin", "success");
                    window.location = c.url
                }
                else 
                    alert(c.message)
            }, "json")
        }
    }
}();


var EditBoard = function(){
    return {
        setup: function(){
            $("#BoardEdit input.collaborator_name").defaultValue($("#BoardEdit input.collaborator_name").val());
            BoardPicker.setup("#BoardEdit .BoardPicker", function(b){
                $("#BoardEdit #id_category").val(b)
            });
            $("#BoardEdit .submit_collaborator").click(function(){
                trackGAEvent("submit_collaborator", "clicked", "edit_board_dialogue");
                if (/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test($("#BoardEdit input.collaborator_name").val())) {
                    $("#InviteCollaborator").show();
                    
                    $("#InviteCollaborator #invite_email").val($("#BoardEdit input.collaborator_name").val());
                    invite_email = $("#BoardEdit input.collaborator_name").val();
                    $("#invite_email, #invite_message").blur();
                    $("#BoardEdit input.collaborator_name").val("Enter a name")
                }
                else {
                    var b = $("#BoardEdit .add_collaborators").find("input").serialize();
                    $.post(board_collaborator, b, function(c){
                        if (c.status == "success") {
                            Tagging.ignore[c.profile_url] = 1;
                            trackGAEvent("submit_collaborator", "success", "edit_board_dialogue");
                            $("#BoardEdit input.collaborator_username").val("");
                            $("#BoardEdit input.collaborator_name").val("");
                            c = '<li class="collaborator unaccepted"><a href="' + c.profile_url + '"><img class="collaborator_image" src="' + c.avatar_url + '" alt="Collaborator Photo" /></a><a class="collaborator_name" href="' + c.profile_url + '">' + c.full_name + '</a><a href="#" class="delete_collaborator" value="' + c.username + '" tooltip="Remove ' + c.full_name + '">x</a><span class="note">Invite sent!</span></li>';
                            c = $(c);
                            $("#BoardEdit .add_collaborators ul").prepend(c);
                            c.find("a.delete_collaborator").tipsy({
                                title: "tooltip",
                                gravity: "s",
                                fade: true,
                                html: true
                            })
                        }
                        else 
                            alert("Looks like something went wrong! We're looking into it. Try again.")
                    })
                }
                return false
            });
            $("body").on("click", "a.delete_collaborator", function(){
                trackGAEvent("delete_colaborator", "clicked", "edit_board_dialogue");
                
                var b = $(this), c = b.hasClass("invite") ? board_collaborator + "delete/" : board_collaborator, e = b.attr("data-name") || b.parent().find(".collaborator_name").text(), g = "Are you sure you want to remove " + e + " from this board?";
                if (e === "yourself") 
                    g = "Are you sure you want to leave this board and remove it from your profile?";
                $("#DeleteCollaborator .message").text(g);
                Modal.show("DeleteCollaborator");
                EditBoard.deleteCollaborator = function(){
                    $.post(c, {
                        collaborator_username: b.attr("value"),
                        remove: true
                    }, function(f){
                        if (f.status == "success") {
                            trackGAEvent("delete_collaborator", "success", "edit_board_dialogue");
                            b.parent().remove();
                            if (e === "yourself") 
                                window.location.href = "/me/";
                            Tagging.ignore && delete Tagging.ignore["/" + b.attr("value") + "/"]
                        }
                        else 
                            alert("Something went wrong. Try Again.");
                        Modal.close("DeleteCollaborator")
                    })
                };
                return false
            });
            $("#invite_submit").submit(function(){
                trackGAEvent("invite_board", "submit", "edit_board_dialogue");
                $.post("/invite/new/", {
                    name: "somebody",
                    email: $("#invite_email").val(),
                    message: $("#invite_message").val(),
                    board_user: board_username,
                    board_name: board_slug
                }, function(b){
                    data = $.parseJSON(b);
                    if (data.status == "success") {
                        trackGAEvent("invite_board", "success", "edit_board_dialogue");
                        $("#invite_name").val("");
                        $("#invite_email").val("");
                        $("#invite_response").html("Invite sent successfully to " + invite_email + ".").show().delay(2E3).fadeOut(500)
                    }
                    else 
                        $("#invite_response").html(data.message)
                });
                return false
            });
            $("#invite_submit").submit(function(){
                var b = 'Hi!\n\nI wanted to invite you to Pinterest so you can help contribute to my pinboard, "' + board_body_name + '". Pinterest is a place to catalog things you love. You can create pinboards on anything, from fashion, to gadgets, to art.\n\nEnjoy!';
                $("#InviteCollaborator").fadeOut(250);
                $("#InviteCollaborator #invite_email").val("");
                $("#InviteCollaborator #invite_message").val(b);
                $("#InviteCollaborator #invite_response").val("")
            })
        },
        init_ac: function(){
            if (!ac_init) {
                ac_init = true;
                Tagging.ignore = {};
                $("#BoardEdit .add_collaborators a.collaborator_name").each(function(b, c){
                    Tagging.ignore[$(c).attr("href")] = 1
                });
                Tagging.initInput("#BoardEdit input.collaborator_name", function(b, c){
                    $("#BoardEdit input.collaborator_username").val(b ? b.value : "");
                    $("#BoardEdit input.collaborator_name").val(b ? b.label : "");
                    c && $("#BoardEdit .submit_collaborator").click()
                }, function(){
                })
            }
        },
        deleteBoard: function(){
            trackGAEvent("delete_board", "clicked", "edit_board_dialogue");
            var b = $("#DeleteBoard .SubmitButton"), c = window.location.pathname.split("/")[1];
            b.addClass("disabled").text("Deleting...");
            $.ajax({
                type: "DELETE",
                dataType: "json",
                url: board_settings,
                success: function(e){
                    trackGAEvent("delete_board", "success", "edit_board_dialogue");
                    if (e.status == "done") 
                        window.location = "/" + c;
                    else 
                        alert("Board delete failed - please refresh and try again. We are very sorry :-/")
                },
                error: function(){
                    alert("Board delete failed - please refresh and try again. We are very sorry :-/")
                }
            })
        }
    }
}();

<!--
var ScrollToTop = ScrollToTop ||
{
    setup: function(){
        var b = 0;//jQuery(window).height() / 2;
        jQuery(window).scroll(function(){
            (window.innerWidth ? window.pageYOffset : document.documentElement.scrollTop) >= b ? jQuery("#ScrollToTop").removeClass("Offscreen") : jQuery("#ScrollToTop").addClass("Offscreen")
        });
        jQuery("#ScrollToTop").click(function(){
            jQuery("html, body").animate({
                scrollTop: "0px"
            }, 400);
            return false
        })
    }
}
-->

var Modal = Modal ||
{
    setup: function(){
        jQuery(document).keydown(function(b){
            if (b.keyCode == 27) {
                var c = jQuery(".ModalContainer:visible").attr("id");
                if (c) 
                    Modal.close(c);
                else 
                    jQuery("#zoomScroll").length && window.history.back();
                b.preventDefault()
            }
        })
    },
    show: function(b){
        var c = jQuery("#" + b), e = jQuery(".modal:first", c), g = c.parent(), f = this;
        c.find(".close").on("click", function(){
            f.trigger("cancel")
        });
        if (g[0] !== jQuery("body")[0]) {
            jQuery("body").append(c);
            c.data("parent", g)
        }
        jQuery("body").addClass("noscroll");
        c.show();
        g = e.outerHeight();
        e.css("margin-bottom", g / 15 + "px");
        setTimeout(function(){
            c.addClass("visible");
            c.css("-webkit-transform", "none")
        }, 1);
        //this.trigger("show", b);
        return false
    },
    close: function(b){
        var c = jQuery("#" + b);
        c.data("parent") && c.data("parent").append(c);
        jQuery("#zoomScroll").length === 0 && jQuery("body").removeClass("noscroll");
        c.removeClass("visible");
        setTimeout(function(){
            c.hide();
            c.css("-webkit-transform", "translateZ(0)")
        }, 251);
        //this.trigger("close", b);
        return false
    }
};


var FancyForm = function(){
    return {
        inputs: ".form input, .form textarea",
        button: ".submitButton",
        setup: function(){
            var b = this;
            this.inputs = $(this.inputs);
            b.inputs.each(function(){
                var c = $(this);
                b.checkVal(c)
            });
            b.inputs.live("keyup blur", function(){
                var c = $(this);
                b.checkVal(c);
                var e = c.parents("ul"), g = c.parents(".form").find(b.button);
                c.parents("li").hasClass("NoCheck") || b.checkDisabled(e, g)
            });
            $(b.button).live("click", function(){
                var c = $(this).attr("data-form");
                if ($(this).hasClass("disabled")) 
                    return false;
                else 
                    $("#" + c + " form").submit()
            })
        },
        checkVal: function(b){
            b.val().length > 0 ? b.parent("li").addClass("val") : b.parent("li").removeClass("val")
        },
        checkDisabled: function(b, c){
            b.children("li:not(.optional)").length <= b.children("li.val").length ? c.removeClass("disabled") : c.addClass("disabled")
        }
    }
}();

var boardPicker = function(){
    return {
        setup: function(b, c, e){
            b = $(b);
            var g = $(".boardListOverlay", b.parent()), f = $(".boardList", b), d = $(".currentBoard", b), h = $("ul", f);
            b.click(function(){
                f.show();
                g.show()
            });
            g.click(function(){
                f.hide();
                g.hide()
            });
            $(h).on("click", "li", function(){
                if (!$(this).hasClass("noSelect")) {
                    d.text($(this).text());
                    g.hide();
                    f.hide();
                    c && c($(this).attr("data"))
                }
                return false
            });
            b = $(".createBoard", f);
            var j = $("input", b), k = $(".button", b), l = $(".createBoardStatus", b);
            k.click(function(){
                if (k.attr("disabled") == "disabled") 
                    return false;
                if (j.val() == "") {
                    //l.html("请输入画板名称").css("color", "red").show();
                    l.html("请输入画板名称").css("color", "red").slideDown(300);
                    return false
                }
                l.html("").hide();
                k.addClass("disabled").attr("disabled", "disabled");
                $.post("/board/create/", {
                    name: j.val(),
                    pass_category: true
                }, function(r){
                    if (r && r.status == "success") {
                        h.append("<li data='" + r.id + "'><span>" + $("<div/>").text(r.name).html() + "</span></li>");
                        f.hide();
                        d.text(r.name);
                        j.val("").blur();
                        k.removeClass("disabled").removeAttr("disabled");
                        e && e(r.id)
                    }
                    else {
                        l.html(r.message).css("color", "red").show();
                        k.removeClass("disabled").removeAttr("disabled")
                    }
                }, "json");
                return false
            })
        }
    }
}();

/*

 var AddDialog = function(){

 return {

 setup: function(b){

 var c = "#" + b, e = $(c), g = $(".buttons .whiteButton", e), f = $(".mainerror", e), d = $(".descriptionTextarea", e);

 BoardPicker.setup(c + " .boardPicker", function(h){

 $(c + " #board_id").val(h)

 }, function(h){

 $(c + " #board_id").val(h)

 });

 AddDialog.shareCheckboxes(b);

 Tagging.initTextarea(c + " .descriptionTextarea");

 Tagging.priceTag(c + " .descriptionTextarea", c + " .imagePicker");

 CharacterCount.setup(c + " .descriptionTextarea", c + " .characterCount", c + " .button");

 g.click(function(){

 if (g.hasClass("disabled"))

 return false;

 trackGAEvent("pin", "clicked", "add_dialogue");

 if (d.val() === "" || d.val() === "描述您的采集...") {

 f.html("请描述您的采集").slideDown(300);

 return false

 }

 else

 f.slideUp(300, function(){

 f.html("")

 });

 g.addClass("disabled").html("Pinning...");

 $("#id_details", e).val(d.val());

 //Tagging.loadTags(c + " .descriptionTextarea", c + " #peeps_holder", c + " #id_tags", c + " #currency_holder");

 $("form", e).ajaxSubmit({

 url: "/pin/create/",

 type: "POST",

 dataType: "json",

 iframe: true,

 success: function(h){

 if (h.status == "success") {

 trackGAEvent("pin", "success", "add_dialogue");

 window.location = h.url

 }

 else

 if (h.captcha) {

 RecaptchaDialog.challenge();

 AddDialog.reset(b)

 }

 else

 f.html(h.message).slideDown(300)

 }

 });

 return false

 })

 },

 reset: function(b){

 b === "createBoard" && CreateBoardDialog.reset();

 b === "scrapePin" && ScrapePinDialog.reset();

 b === "uploadPin" && UploadPinDialog.reset();

 AddDialog._resets[b] && AddDialog._resets[b]()

 },

 close: function(b, c){

 $("#" + b).addClass("super");

 Modal.show(c)

 },

 childClose: function(b, c){

 var e = this, g = $("#" + c);

 $(".ModalContainer", g);

 e.reset(c);

 $("#" + b).removeClass("super");

 Modal.close(b);

 Modal.close(c)

 },

 pinBottom: function(b){

 var c = $("#" + b);

 $(".pinBottom", c).slideDown(300, function(){

 var e = $(".modal:first", c);

 e.css("margin-bottom", "-" + e.outerHeight() / 2 + "px")

 })

 },

 shareCheckboxes: function(b){

 function c(f){

 var d = $("#" + b), h = $(".publish_to_" + f, d), j = $("#id_publish_to_" + f, d);

 h.change(function(){

 if (h.is(":checked")) {

 j.attr("checked", "checked");

 h.parent().addClass("active")

 }

 else {

 j.removeAttr("checked");

 h.parent().removeClass("active")

 }

 });

 var k = h.is(":checked");

 return function(){

 if (k) {

 h.parent().addClass("active");

 h.attr("checked", "checked")

 }

 else {

 h.parent().removeClass("active");

 h.removeAttr("checked")

 }

 }

 }

 //var e = c("facebook"), g = c("twitter");

 AddDialog._resets = AddDialog._resets || {};

 AddDialog._resets[b] = function(){

 e();

 g()

 }

 }

 }

 }();

 var Tagging = function(){

 return {

 friends: null,

 friendsLinks: {},

 getFriends: function(b, c, e){

 var g = b.term;

 (function(f){

 Tagging.friends ? f() : $.get("/x2ns4tdf0cd7cc9b/_getfriends/", function(d){

 Tagging.friends = [];

 $.each(d, function(h, j){

 Tagging.friends.push({

 label: j.name,

 value: j.username,

 image: j.image,

 link: "/" + j.username + "/",

 category: "People"

 });

 Tagging.friendsLinks["/" + j.username + "/"] = 1

 });

 f()

 })

 })(function(){

 var f = [];

 if (e)

 for (name in e)

 Tagging.friendsLinks[name] || !e.hasOwnProperty(name) || f.push(e[name]);

 f = f.concat(Tagging.friends);

 if (Tagging.ignore)

 f = _.filter(f, function(d){

 return !Tagging.ignore[d.link]

 });

 if (g)

 f = tagmate.filter_options(f, g);

 c(f)

 })

 },

 initInput: function(b, c, e){

 b = $(b);

 var g = $("<div class='CollabAutocompleteHolder'></div>");

 b.after(g);

 b.autocomplete({

 source: Tagging.getFriends,

 minLength: 1,

 delay: 5,

 appendTo: g,

 change: function(f, d){

 c && c(d.item)

 },

 select: function(f, d){

 c && c(d.item, true);

 return false

 },

 position: {

 my: "left top",

 at: "left bottom",

 offset: "0 -1"

 }

 }).keydown(function(f){

 f.which == 13 && e && e()

 });

 b.data("autocomplete")._renderItem = function(f, d){

 return $("<li></li>").data("item.autocomplete", d).append("<a href='" + d.link + "'><img src='" + d.image + "' class='AutocompletePhoto' alt='Photo of " + d.label + "' width='38px' height='38px'/><span class='AutocompleteName'>" + d.label + "</span></a>").appendTo(f)

 }

 },

 initTextarea: function(b, c){

 b = $(b);

 var e = {};

 e["@"] = tagmate.USER_TAG_EXPR;

 e["#"] = tagmate.HASH_TAG_EXPR;

 e.$ = tagmate.USD_TAG_EXPR;

 e["\u00a3"] = tagmate.GBP_TAG_EXPR;

 b.tagmate({

 tagchars: e,

 sources: {

 "@": function(g, f){

 Tagging.getFriends(g, f, c)

 }

 }

 })

 },

 loadTags: function(b, c, e, g){

 b = $(b).getTags();

 for (var f = [], d = [], h = null, j = 0; j < b.length; j++) {

 b[j][0] == "@" && f.push(b[j].substr(1));

 b[j][0] == "#" && d.push(b[j].substr(1));

 if (b[j][0] == "$" || b[j][0] == "\u00a3")

 h = b[j]

 }

 $(c).val(f.join(","));

 $(e).val(d.join(","));

 $(g).val(h)

 },

 priceTag: function(b, c){

 function e(){

 var g = $(".price", c);

 if (g.length <= 0) {

 g = $("<div class='price'></div>");

 c.prepend(g)

 }

 var f = b.getTags({

 $: tagmate.USD_TAG_EXPR,

 "\u00a3": tagmate.GBP_TAG_EXPR

 });

 

 if (f && f.length > 0) {

 g.text(f[f.length - 1]);

 g.addClass("visible")

 }

 else {

 g.removeClass("visible");

 g.text("")

 }

 }

 b = $(b);

 c = $(c);

 b.unbind(".priceTag").bind("keyup.priceTag", e).bind("focus.priceTag", e).bind("change.priceTag", e);

 e()

 }

 }

 }();

 */

